//
//  MXSStatusMajorView.swift
//  weibo
//
//  Created by 孙飞 on 15/12/12.
//  Copyright © 2015年 Sunfei. All rights reserved.
//

import UIKit
import SnapKit

class MXSStatusMajorView: UIView {
    
    //底部约束
    var bottomConstraint: Constraint?
    
    var statusViewModel: MXSStatusVM? {
        didSet{
            //头像
            iconView.sd_setImageWithURL(NSURL(string: (statusViewModel?.status?.user?.profile_image_url)!), placeholderImage: UIImage(named: "avatar_default_big"))
            //昵称
            nameLabel.text = statusViewModel?.status?.user?.name
            
            // ******* 时间显示处理
//            timeLabel.text = "刚刚"
//            sourceLabel.text = "来自 超级微博"
            sourceLabel.text = statusViewModel?.sourceText
            timeLabel.text = statusViewModel?.createAtText
            
            //图标
            vipImageView.image = statusViewModel?.vipImage
            verifiedImageView.image = statusViewModel?.verifiedImage
            
            //原创微博
            contentLabel.text = statusViewModel?.status?.text
            
            // 设置原创微博的配图
            bottomConstraint?.uninstall()
            // 如果有配图:
            //1.设置数据,2.显示控件,3.更新约束
            if let pic_urls = statusViewModel?.status?.pic_urls where pic_urls.count > 0 {
                // 设置数据
                pictureView.pic_urls = pic_urls
                // 显示控件
                pictureView.hidden = false
                // 更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraint = make.bottom.equalTo(pictureView).offset(MXSCellMargin).constraint
                })
            }else{
                pictureView.hidden = true
                // 更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraint = make.bottom.equalTo(contentLabel).offset(MXSCellMargin).constraint
                })
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    // MARK: - 懒加载控件
    // 头像
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    
    //昵称
    private lazy var nameLabel: UILabel = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 14)
    //时间
    private lazy var timeLabel: UILabel = UILabel(textColor: UIColor.orangeColor(), fontSize: 13)
    private lazy var sourceLabel: UILabel = UILabel(textColor: UIColor.orangeColor(), fontSize: 13)
    
    // 会员图标
    private lazy var vipImageView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    // 认证
    private lazy var verifiedImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    // 微博内容
    private lazy var contentLabel: UILabel = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 15, maxWidth: SCREENW - 2 * MXSCellMargin)
    // 初始化一个原创微博的配图控件
    private lazy var pictureView: MXSStatusPicView = {
        let view = MXSStatusPicView()
        view.backgroundColor = self.backgroundColor
        return view
    }()
    
    
    private func setupUI(){
        // 1. 添加控件
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(vipImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(verifiedImageView)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        
        // 2. 添加约束
        //头像
        iconView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self).offset(MXSCellMargin)
            make.leading.equalTo(self).offset(MXSCellMargin)
            make.size.equalTo(CGSizeMake(35, 35))
        }
        //昵称
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_top)
            make.leading.equalTo(iconView.snp_trailing).offset(MXSCellMargin)
        }
        
        // 会员图标
        vipImageView.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(nameLabel.snp_centerY)
            make.leading.equalTo(nameLabel.snp_trailing).offset(MXSCellMargin)
        }
        // 时间
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconView.snp_bottom)
            make.leading.equalTo(nameLabel)
        }
        // 来源
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(timeLabel.snp_centerY)
            make.leading.equalTo(timeLabel.snp_trailing).offset(MXSCellMargin)
        }
        // 认证
        verifiedImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_trailing)
            make.centerY.equalTo(iconView.snp_bottom)
        }
        
        // 原创微博内容
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView.snp_leading)
            make.top.equalTo(iconView.snp_bottom).offset(MXSCellMargin)
        }
        
        //配图
        pictureView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp_bottom).offset(MXSCellMargin)
            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        
        
        
        
        //用最后一个控件约束 确定cell高度的关键
        snp_makeConstraints { (make) -> Void in
            self.bottomConstraint = make.bottom.equalTo(pictureView).offset(MXSCellMargin).constraint
        }

    }
    
    
    
}



